package com.yycx.module.bus.provider.service.hy.impl;

import com.yycx.common.base.entity.EntityMap;
import com.yycx.common.mybatis.base.service.impl.BaseServiceImpl;
import com.yycx.common.mybatis.model.ResultBody;
import com.yycx.common.mybatis.query.CriteriaQuery;
import com.yycx.common.mybatis.query.CriteriaSave;
import com.yycx.common.mybatis.query.CriteriaUpdate;
import com.yycx.common.security.OpenHelper;
import com.yycx.module.bus.client.entity.hy.BusPolicyPosition;
import com.yycx.module.bus.provider.mapper.hy.BusPolicyPositionMapper;
import com.yycx.module.bus.provider.service.hy.BusPolicyPositionService;
import com.yycx.module.user.client.entity.AppPosition;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 政策职务配置接口实现类
 *
 * @author flyme
 * @date 2023-08-15
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class BusPolicyPositionServiceImpl extends BaseServiceImpl<BusPolicyPositionMapper, BusPolicyPosition> implements BusPolicyPositionService {


    @Override
    public ResultBody beforeAdd(CriteriaSave cs, BusPolicyPosition bpp, EntityMap extra) {
        bpp.setCompanyId(OpenHelper.getCompanyId());
        return ResultBody.ok();
    }

    @Override
    public ResultBody beforeEdit(CriteriaUpdate<BusPolicyPosition> cu, BusPolicyPosition bpp, EntityMap extra) {
        return super.beforeEdit(cu, bpp, extra);
    }

    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
    public ResultBody beforePageList(CriteriaQuery<BusPolicyPosition> cq, BusPolicyPosition bpp, EntityMap requestMap) {
        cq.select(BusPolicyPosition.class, "*");
        cq.eq(BusPolicyPosition.class, "policyDrugId");
        cq.addSelect(AppPosition.class, "positionName");
        cq.createJoin(AppPosition.class);
        cq.orderByDesc("bpp.createTime");
        return ResultBody.ok();
    }


    @Override
    public BusPolicyPosition findBusPolicyPosition(Long policyDrugId, Long positionId, Date sendDate) {
        CriteriaQuery<BusPolicyPosition> cq = new CriteriaQuery(BusPolicyPosition.class);
        cq.eq(true, "policyDrugId", policyDrugId);
        cq.eq(true, "positionId", positionId);
        cq.and(e -> e.isNull("endDate").or().ge(true, "endDate", sendDate));
        return getOne(cq,false);
    }

    @Override
    public List<BusPolicyPosition> selectPolicyPositionList(Long policyDrugId) {
        CriteriaQuery<BusPolicyPosition> cq = new CriteriaQuery(BusPolicyPosition.class);
        cq.eq(true, "policyDrugId", policyDrugId);
        cq.isNull("endDate");
        return list(cq);
    }

}
